package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.responseresult.CarTeamMenmberData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.StringUtil;

import java.util.ArrayList;


public class ChooseDriverAdapter extends BaseListAdapter<CarTeamMenmberData> {//首页竞价大厅的adapter
    private Activity activity;
    private ArrayList<Boolean> imgs=new ArrayList<Boolean>();
   private int driverId=-1;
    private int choosePosition=-1;
    public int getDriverId() {
        return driverId;
    }

    public ChooseDriverAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity=activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_choose_driver, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.iv_header = (ImageView)convertView.findViewById(R.id.iv_header);
            viewHolder.tv_driver_name = (TextView) convertView.findViewById(R.id.tv_driver_name);
            viewHolder.tv_car_band = (TextView) convertView.findViewById(R.id.tv_car_band);
            viewHolder.tv_car_band_num = (TextView) convertView.findViewById(R.id.tv_car_band_num);
            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            viewHolder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
      final   CarTeamMenmberData  data= getItem(position);

        BitmapUtil.showImgForNetPath(data.getUserImage(),viewHolder.iv_header);
        viewHolder.tv_driver_name.setText(data.getUsername());
        viewHolder.tv_car_band_num.setText(data.getPlateNumber());
        viewHolder.tv_weight.setText(StringUtil.getDouble(data.getCapacity()+"")+"t");

        viewHolder.iv_choose.setSelected(false);

        if (position==choosePosition){
            viewHolder.iv_choose.setSelected(true);
        }else {
            viewHolder.iv_choose.setSelected(false);
        }

        viewHolder.iv_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                driverId=data.getDriverId();
                choosePosition=position;
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView iv_header;
        TextView tv_driver_name;
        TextView tv_car_band;
        TextView tv_car_band_num;
        TextView tv_weight;
        ImageView iv_choose;

    }
}
